<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['flowable:appVersion:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="appVersionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"  />
      <el-table-column label="版本ID" align="center" prop="versionId"   />
      <el-table-column label="事项" align="center">
        {{ appInfo.appName }}
      </el-table-column>
      <el-table-column label="版本" align="center" prop="version" width="80">
        <template slot-scope="scope">
          <el-tag v-if="appInfo.useVersion == scope.row.versionId" style="color:red;">V{{scope.row.version}}</el-tag>
          <el-tag v-else>V{{scope.row.version}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="表单版本" align="center" prop="formVersionId">
        <template slot-scope="scope">
          {{ formVersionMap[scope.row.formVersionId] }}
        </template>
      </el-table-column>
      <el-table-column label="流程部署ID" align="center" prop="deployId" />
      <el-table-column label="流程图" align="center" prop="bpmnXml"   width="80">
        <template slot-scope="scope">
          <el-button
            :disabled="!scope.row.bpmnXml"
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="viewBpmnForm(scope.row)"
          >流程图</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-news"
            @click="designer(scope.row)"
            v-hasPermi="['flowable:appVersion:edit']"
          >流程设计</el-button>
          <el-button
            v-if="scope.row.bpmnXml && !scope.row.deployId"
            size="mini"
            type="text"
            icon="el-icon-connection"
            @click="deployBpmn(scope.row)"
            v-hasPermi="['flowable:appVersion:deployBpmn']"
          >部署</el-button>
          <el-button
            v-if="appInfo.useVersion != scope.row.versionId && scope.row.deployId"
            size="mini"
            type="text"
            icon="el-icon-star-off"
            @click="setUseVersion(scope.row)"
            v-hasPermi="['flowable:appVersion:setUseVersion']"
          >使用版本</el-button>
          <el-button
            v-if="scope.row.deployId"
            size="mini"
            type="text"
            icon="el-icon-document-copy"
            @click="copyVersion(scope.row)"
            v-hasPermi="['flowable:appVersion:copyVersion']"
          >复制</el-button>
          <el-button
            v-if="!scope.row.deployId"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['flowable:appVersion:edit']"
          >修改</el-button>
          <el-button
            v-if="!scope.row.deployId"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['flowable:appVersion:remove']"
          >删除</el-button>

          <!-- <el-dropdown size="mini" @command="(command) => handleCommand(command, scope.row)" v-hasPermi="['flowable:appVersion:edit','flowable:appVersion:remove']">
              <span class="el-dropdown-link">
                <i class="el-icon-d-arrow-right el-icon--right"></i>更多
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="handleUpdate" icon="el-icon-edit"
                  v-hasPermi="['flowable:appVersion:edit']">修改</el-dropdown-item>
                <el-dropdown-item command="handleDelete" icon="el-icon-delete"
                  v-hasPermi="['flowable:appVersion:remove']">删除</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改事项版本对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="表单版本" prop="formId">
          <el-select v-model="form.formVersionId" placeholder="请选择表单版本">
            <el-option
              v-for="item in formVersionList"
              :key="item.versionId"
              :label="item.formName" :value="item.versionId"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="流程图" :visible.sync="show" width="60%" append-to-body>
      <flow-view v-if="xmlData" :xmlData.sync="xmlData" :taskList.sync="taskList"></flow-view>
    </el-dialog>
  </div>
</template>

<script>
import { listAppVersion, getAppVersion, delAppVersion, addAppVersion, updateAppVersion, deployBpmn, copyVersion, setUseVersion } from "@/api/flowable/appVersion";
import { getApp, getFormVersionList} from "@/api/flowable/app";
import FlowView from "@/views/flowable/bpmn/FlowView"

export default {
  name: "AppVersion",
  components:{FlowView},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 事项版本表格数据
      appVersionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        formVersionId: null,
        deployId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      appId: this.$route.query.appId,
      appInfo: {},
      formVersionList:[],
      formVersionMap: {},
      show:false,
      xmlData: '',
      taskList: {},
    };
  },
  created() {
    this.getApp();
  },
  methods: {
    getApp(){
      getApp(this.appId).then(res => {
        this.appInfo = res.data;
        getFormVersionList(res.data.formId).then(res2 =>{
          this.formVersionList = res2.data;
          res2.data.forEach(item=>{
            this.formVersionMap[item.versionId] = item.formName;
          })
          this.getList();
        })
      })
    },
    /** 查询事项版本列表 */
    getList() {
      this.loading = true;
      this.queryParams.appId = this.appId;
      listAppVersion(this.queryParams).then(response => {
        this.appVersionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        versionId: null,
        appId: this.appId,
        formVersionId: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.versionId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加事项版本";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const versionId = row.versionId || this.ids
      getAppVersion(versionId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改事项版本";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.versionId != null) {
            updateAppVersion(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAppVersion(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const versionIds = row.versionId || this.ids;
      this.$modal.confirm('是否确认删除事项版本编号为"' + versionIds + '"的数据项？').then(function() {
        return delAppVersion(versionIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('flowable/appVersion/export', {
        ...this.queryParams
      }, `appVersion_${new Date().getTime()}.xlsx`)
    },
    designer(row){
      this.$router.push("/flowable/designer?appVersionId="+row.versionId+'&formVersionId='+row.formVersionId+'&appName='+this.appInfo.appName)
    },
    deployBpmn(row){
      this.$modal.confirm('确认部署该流程图？ 注意：部署后流程图不能修改，只能修改节点表单权限').then(function() {
        return  deployBpmn(row.versionId)
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => {});
    },
    viewBpmnForm(row){
      this.show = true;
      this.xmlData = row.bpmnXml;
    },
    copyVersion(row){
      this.$modal.confirm('确认复制该版本？').then(function() {
        return  copyVersion(row.versionId)
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => {});
    },
    setUseVersion(row){
      this.$modal.confirm('设置为发起版本？').then(function() {
        return  setUseVersion(row.versionId)
      }).then(() => {
        this.getApp();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => {});
    },
    // // 更多操作触发
    // handleCommand(command, row) {
    //   switch (command) {
    //     case "handleUpdate":
    //       this.handleUpdate(row);
    //       break;
    //     case "handleDelete":
    //       this.handleDelete(row);
    //       break;
    //     default:
    //       break;
    //   }
    // },
  }
};
</script>
